<?php

namespace app\controller\admin\sky;

use app\model\admin\sky\SkyMagicData;
use app\model\admin\sky\SkyMagicDetail;
use app\model\admin\sky\SkyMagic;
use laytp\controller\Backend;
use think\facade\Db;
use think\response\Json;

/**
 * Class Magic
 * @package app\controller\admin\sky
 */
class Magic extends Backend
{
    /**
     *
     * @var Magic
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'desc'];

    public function _initialize()
    {
        $this->model = new SkyMagic();
    }

    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function index()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $allData = $this->request->param('all_data');
        $post = $this->request->param();
        if(isset($post['search_param']['magic_name']['value'])&&!empty($post['search_param']['magic_name']['value'])){

            $magic_data_ids = (new SkyMagicData())->where("magic_name","like","%".$post['search_param']['magic_name']['value']."%")->column("id");
            $magic_ids = (new SkyMagicDetail())->whereIn("magic_data_id",$magic_data_ids)->column("magic_id");
            $where[] = [
              'id',
              'in',
              $magic_ids
            ];
        }
        $where[] = [
            'type',
            '>',
            0
        ];
        if ($allData) {
            $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        } else {
            $limit = $this->request->param('limit', 10);
            $data  = $this->model->with("magicData")->where($where)->order($order)->paginate($limit)->toArray();
            foreach ($data['data'] as &$item){
               $item['magic_name'] = array_column($item['magicData'],"magic_name");
               if(!empty($item['magic_name'])){
                   $item['magic_name'] = implode(",",$item['magic_name']);
               }else{
                   $item['magic_name'] = "";
               }
               $item['magic_img'] = array_column($item['magicData'],"magic_img");
                if(!empty($item['magic_img'])){
                    $item['magic_img'] = implode(", ",$item['magic_img']);
                }else{
                    $item['magic_img'] = "";
                }
            }
        }
        return $this->success('数据获取成功', $data);
    }

    public function info(){
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        $ids = (new SkyMagicDetail())->where("magic_id",$id)->column("magic_data_id");
        $info["magic_data_ids"] = implode(",",$ids);
        return $this->success('获取成功', $info);
    }

    public function edit(){
        $id   = $this->request->param('id');
        $magic_ids   = $this->request->param('magic_ids');
        $magic_ids = explode(",",$magic_ids);
        array_walk($magic_ids,function (&$item)use($id){
            $item = [
              'magic_id' =>$id,
              'magic_data_id' =>$item,
            ];
        });
        $magic_detail_model = (new SkyMagicDetail());
        Db::startTrans();
        $magic_detail_model->where('magic_id',$id)->delete();
        $magic_detail_model->insertAll($magic_ids);
        Db::commit();
        return $this->success('修改成功');
    }
}